package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.CommandQueryVo;
import com.moli.iov.admin.service.IVehicleLogFacadeService;
import com.moli.iov.down.model.dto.req.VehicleCommandQueryDto;
import com.moli.iov.down.model.dto.res.VehicleCommandResDto;
import com.moli.iov.down.remote.IVehicleCommandLogRemoteService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 控车日志
 * </p>
 *
 * @author zql
 * @since 2019/9/19 14:31
 */
@Slf4j
@Service
public class VehicleLogFacadeServiceImpl implements IVehicleLogFacadeService {

  @Reference(group = "${service.group}", check = false)
  private IVehicleCommandLogRemoteService vehicleCommandLogRemoteService;

  /**
   * 车辆控制、设置日志查询
   * @param commandQueryVo
   * @return
   */
  @Override
  public RestResponse<PageExtend<VehicleCommandResDto>> listVehicleCommandLog(
      CommandQueryVo commandQueryVo) {
    return vehicleCommandLogRemoteService.listVehicleCommand(convert(commandQueryVo));
  }

  /**
   * 车辆控制日志导出
   * @param commandQueryVo
   * @return
   */
  @Override
  public void vehicleControllExport(CommandQueryVo commandQueryVo,
      HttpServletRequest req, HttpServletResponse res) throws Exception {
    commandQueryVo.setPageNum(Constants.EXPORT_PAGE_NUM);
    commandQueryVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<VehicleCommandResDto>> restResponse = listVehicleCommandLog(commandQueryVo);
    List<VehicleCommandResDto> vehicleCommands = new ArrayList<>();
    if(restResponse.flag){
      vehicleCommands = restResponse.getData().getRecords();
    }

    final String[] propNames = {"vin","license", "companyName", "operateTypeName","statusStr","resultStr",
        "resultMemo", "memo", "sysUserName","createTime"};
    final String[] headStrs = {"车架号","车牌号","所属公司","操作类型","下发状态",
        "执行结果","执行结果描述", "控车原因","操作人", "操作时间"};
    String title = "车辆控制日志";
    ExcelUtil.exportDataExcel(req, res, vehicleCommands, propNames, headStrs, title);
  }


  /**
   * 车辆设置日志导出
   * @param commandQueryVo
   * @return
   */
  @Override
  public void vehicleSetExport(CommandQueryVo commandQueryVo,
      HttpServletRequest req, HttpServletResponse res) throws Exception {
    commandQueryVo.setPageNum(Constants.EXPORT_PAGE_NUM);
    commandQueryVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<VehicleCommandResDto>> restResponse = listVehicleCommandLog(commandQueryVo);
    List<VehicleCommandResDto> vehicleCommands = new ArrayList<>();
    if(restResponse.flag){
      vehicleCommands = restResponse.getData().getRecords();
    }

    final String[] propNames = {"vin", "companyName","iccid", "operateTypeName","statusStr","resultStr",
        "resultMemo", "sysUserName","createTime"};
    final String[] headStrs = {"车架号","所属公司","ICCID","操作类型","下发状态",
        "执行结果","执行结果描述","操作人", "操作时间"};
    String title = "终端设置日志";
    ExcelUtil.exportDataExcel(req, res, vehicleCommands, propNames, headStrs, title);
  }

  /**
   * 转换成 CommandQueryVo
   * @param commandQueryVo
   * @return
   */
  private VehicleCommandQueryDto convert(CommandQueryVo commandQueryVo) {
    VehicleCommandQueryDto vehicleCommandQueryDto = new VehicleCommandQueryDto();
    CommonUtils.copyProperties(commandQueryVo, vehicleCommandQueryDto);
    return vehicleCommandQueryDto;
  }
}
